function wavchop(wname,wnewname)
% Extract meaningful part of wav-file recorded via microphone
%   ie, chops off 'audio space' from beginning & end
% Copyright 1997 Jonathan Cline
%
%       This program is free software; you can redistribute it and/or
%       modify it under the terms of the GNU General Public License
%       as published by the Free Software Foundation; either version 2
%       of the License, or (at your option) any later version.
%
%       This program is distributed in the hope that it will be useful,
%       but WITHOUT ANY WARRANTY; without even the implied warranty of
%       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%       GNU General Public License for more details.
%
%       To obtain a copy of the GNU General Public License
%       write to the Free Software
%       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
%       02111-1307, USA,
%       or download it at http://www.gnu.org/copyleft/gpl.html.
%
% Example usage:  wavchop('myfile', 'newfile')
% version 2:00PM  6/3/1997

if nargin~=2
        error('WAVCHOP takes two arguments, ');
	error('which are the names of the .WAV files');
end

if isempty(findstr(wname,'.')) %==[]
        wname=[wname,'.wav'];
end

if isempty(findstr(wnewname,'.')) %==[]
        wnewname=[wnewname,'.wav'];
end

[wsave,Fs,fmt]=wavread(wname);

w=wsave;

% Normalize
wmean=mean(w);
%w=w-wmean; Don't do this
wmax=max(abs(w));
%w=w/wmax;  Or this either

% Extract signal, ie, remove near-0 parts at beginning & end
% actually trims parts @ beg & end less than 15% of absolute max value
wstart=0;
for k=1:size(w)
   if abs(w(k))>wmean+15*wmax/100 & wstart==0
   	wstart=k;
   end;
end
wstop=size(w);
for k=size(w):-1:1
        if abs(w(k))>wmean+15*wmax/100 & wstop==size(w)
                wstop=k;
        end
end;

wnew=w(wstart:wstop);

wavwrite(wnew,Fs,wnewname);

% end matlab source